#include "Scene.h"
#include "Block.h"
#include "Player.h"
#include "Enemy.h"
#include "Castle.h"

Scene::Scene()
{
	//level balken
	Block* ba1 = new Block( Position(0, -650) );
	ba1->setSize(250,500);
	m_entities.push_back( ba1 );

	Block* ba2 = new Block( Position(260, -130) );
	ba2->setSize(120,5);
	ba2->setColor(5);
	m_entities.push_back( ba2 );

	Block* ba3 = new Block( Position(390, -750) );
	ba3->setSize(500,500);
	m_entities.push_back( ba3 );

	Block* ba4 = new Block( Position(390, -160) );
	ba4->setColor(8);
	m_entities.push_back( ba4 );

	Block* ba5 = new Block( Position(410, -190) );
	ba5->setColor(8);
	m_entities.push_back( ba5 );

	Block* ba6 = new Block( Position(900, -720) );
	ba6->setColor(4);
	ba6->setSize(200,500);
	m_entities.push_back( ba6 );

	Block* ba7 = new Block( Position(1150, -200) );
	ba7->setColor(8);
	m_entities.push_back( ba7 );

	Block* ba8 = new Block( Position(1200, -200) );
	ba8->setColor(8);
	m_entities.push_back( ba8 );

	Block* ba9 = new Block( Position(1250, -200) );
	ba9->setColor(8);
	m_entities.push_back( ba9 );

	Block* ba10 = new Block( Position(1300, -190) );
	ba10->setColor(8);
	m_entities.push_back( ba10 );

	Block* ba11 = new Block( Position(1350, -240) );
	ba11->setColor(7);
	m_entities.push_back( ba11 );

	Block* ba12 = new Block( Position(1400, -230) );
	ba12->setColor(5);
	ba12->setSize(500,30);
	m_entities.push_back( ba12 );

	Castle* c = new Castle( Position(1860, -200) );
	m_entities.push_back( c );

	//blokjes
	Block* b = new Block( Position(0, -120) );
	b->setColor(6);
	m_entities.push_back( b );

	Block* b1 = new Block( Position(40, -120) );
	b1->setColor(6);
	m_entities.push_back( b1 );

	Block* b2 = new Block( Position(80, -120) );
	b2->setColor(6);
	m_entities.push_back( b2 );

	Block* b3 = new Block( Position(120, -120) );
	b3->setColor(7);
	m_entities.push_back( b3 );

	Block* b4 = new Block( Position(160, -120) );
	b4->setColor(7);
	m_entities.push_back( b4 );

	Block* b5 = new Block( Position(200, -120) );
	b5->setColor(7);
	m_entities.push_back( b5 );

	//Block* b6 = new Block( Position(240, -120) );
	//b6->setColor(8);
	//m_entities.push_back( b6 );

	//enemies 
	for( int i = 0; i < 500; i++)
	{
		Enemy* x = new Enemy( Position(160 + ( i * 60), 0) );
		m_entities.push_back( x );
	}

	m_player = new Player(Position(5, -145));
	m_entities.push_back( m_player );
}

void Scene::update( float frametime )
{
	// Update all entities
	std::vector<Entity*>::iterator it, it2;
	Entity* ent;
		Entity* ent2;
		for( it = m_entities.begin(); it != m_entities.end(); it++ )
		{
			ent = (*it);

			if( ent == m_player || ent->shouldUpdate(m_player->getPosition()) )
			{
				for( it2 = m_entities.begin(); it2 != m_entities.end(); it2++ )
				{
					ent2 = (*it2);

					if( ent == ent2 )
						continue;

					int side = ent->intersects(ent2);
					if(side > 0)
					{
						ent->setIntersect(ent2, side);
					}
				}

				ent->update( frametime );
			}

			if( ent->toDelete() )
			{
				delete ent;
				m_entities.erase( it );
			it--;
		}
	}
}